package com.example.springbootdemo.controller;


import com.example.springbootdemo.po.Dept;
import com.example.springbootdemo.po.Emp;
import com.example.springbootdemo.service.EmpService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@Api(tags = "员工信息管理控制器")
public class EmpController {
    @Autowired
    private EmpService empService;

    @RequestMapping("/empIndex")
    @ApiOperation("员工系统首页")
    public String empIndex(Model model){
        List<Emp> list = this.empService.queryEmp(new Emp());
        model.addAttribute("list",list);
        return "empIndex";
    }
    @RequestMapping("/gotoAddEmp")
    @ApiOperation("跳转添加员工页面")
    public String gotAddEmp() {
        return "addEmp";
    }
    @RequestMapping("/saveEmp")
    @ApiOperation("保存员工信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "emp",value = "员工对象",dataType = "Emp")
    })
            public String saveEmp(Emp emp,Model model){
            this.empService.addEmp(emp);
            return this.empIndex(model);
        }
    @RequestMapping("/gotoEditEmp/{empNo}")
    public String gotoEditEmp(@PathVariable("empNo") String empNo, Model model){
        System.out.println("============"+empNo);
        Emp emp = new Emp();
        emp.setEmpNo(Integer.valueOf(empNo));
        //根据部门编号有且只有一个
        List<Emp> list = this.empService.queryEmp(emp);
        model.addAttribute("emp",list.get(0));
        return "updateEmp";
    }

    @PostMapping("/updateEmp")
    @ApiOperation("更新员工信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "emp",value = "员工对象",dataType = "Emp")
    })
    public String updateEmp(Emp emp, Model model){
        this.empService.updateEmp(emp);
        return this.empIndex(model);
    }
    @GetMapping("/delEmp")
    @ApiOperation("删除员工信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "emp",value = "删除员工",dataType = "String")
    })
    public String delEmp(String empNo,Model model){
        this.empService.delEmp(Integer.valueOf(empNo));
        return this.empIndex(model);
    }
    @PostMapping("/queryEmp")
    public String queryEmp(Emp emp,Model model){
        //构造模糊查询条件
        if(emp.getEmpName() != null && !emp.getEmpName().equals("")){
            emp.setEmpName("%" + emp.getEmpName() + "%");
        }

        List<Emp> list = this.empService.queryEmp(emp);
        model.addAttribute("list",list);
        return "empIndex";
    }
    //异步操作
    @RequestMapping("/ajaxQueryEmp")
    @ResponseBody
    public List<Emp> ajaxQueryEmp(){
        List<Emp> list = this.empService.queryEmp(new Emp());
        return list;
    }
    //异步添加员工
    @PostMapping("/ajaxSaveEmp")
    @ResponseBody
    public String ajaxSaveEmp(@RequestBody Emp emp){
        this.empService.addEmp(emp);
        return "sucess";
    }
    //异步删除

    @GetMapping("/ajaxDelEmp/{empNo}")
    @ResponseBody
    public String ajaxDelEmp (@PathVariable String empNo){
        this.empService.delEmp(Integer.valueOf(empNo));
        return "sucess";
    }
    //异步调用查询部门信息
    @GetMapping("/ajaxQueryEmpUp/{empNo}")
    @ResponseBody
    public Emp ajaxQueryEmpUp(@PathVariable String empNo){
        Emp emp = new Emp();
        emp.setEmpNo(Integer.valueOf(empNo));
        List<Emp> list = this.empService.queryEmp(emp);
        return list.get(0);
    }
    //异步修改
    @PostMapping("/ajaxUpdateEmp")
    @ResponseBody
    public String ajaxUpdateEmp(@RequestBody Emp emp){
        this.empService.updateEmp(emp);
        return "sucess";
    }
    }

